import smtplib
from email.mime.text import MIMEText
from email.header import Header
from .config import *
from celery_tasks.main import celery_app


@celery_app.task(name='send_email')
def send_email(to_email, verify_url):
    subject = " 商城邮箱验证"
    # 发送内容:
    html_message = '<p>尊敬的用户您好！</p>' \
                   '<p>感谢您使用 商城。</p>' \
                   '<p>您的邮箱为：%s 。请点击此链接激活您的邮箱：</p>' \
                   '<p><a href="%s">%s<a></p>' % (to_email, verify_url, verify_url)

    message = MIMEText(html_message, 'plain', 'utf-8')
    message['From'] = Header(EMAIL_FROM, 'utf-8')
    message['To'] = Header("用户", 'utf-8')
    message['Subject'] = Header(subject, 'utf-8')

    smtpObj = smtplib.SMTP()
    smtpObj.connect(EMAIL_HOST, 25)  # 25 为 SMTP 端口号
    smtpObj.login(EMAIL_HOST_USER, EMAIL_HOST_PASSWORD)
    smtpObj.sendmail(EMAIL_FROM, [to_email], message.as_string())


if __name__ == '__main__':
    send_email(to_email='sxy122333@outlook.com', verify_url='verify_url')
